package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
 import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;



    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }




    //根据id查询套餐
    @Override
    public SetmealVO getById(Long id) {
        //1.先根据id查询出来套餐表里得数据
        Setmeal setmeal = setmealMapper.getById(id);
        //2.创建vo对象
        SetmealVO setmealVO = new SetmealVO();
        //复制数据
        BeanUtils.copyProperties(setmeal,setmealVO);

        return setmealVO;
    }

    //新增套餐
    @Override
    @Transactional
    public void Insert(SetmealDTO setmealDTO) {
        //因为新增套餐 要添加菜品
        //所以先创建一个套餐对象
        Setmeal setmeal = new Setmeal();
        //把dto对象数据复制到套餐对象中
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //因为新建得套餐是默认停售得，所以给它设置默认值
        setmeal.setStatus(StatusConstant.DISABLE);
        //把数据插入到套餐表中
        setmealMapper.Insert(setmeal);  //TODO:让主键回显

        //菜品
        //因为套餐表和口味表是多对多关系，菜品表是从表，插入数据需要套餐表得id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //判断setmealDishes是否有数据
        if (setmealDishes != null && setmealDishes.size()>0){
            //遍历：将setmealid给到setmealDishes
            setmealDishes.forEach(setmealDishe->{
                setmealDishe.setSetmealId(setmealId);
            });
            //往数据setmealDish表插入数据
            setmealDishMapper.insert(setmealDishes);
        }
    }


    //分页查询
    @Override
    public PageResult getPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

      Page<SetmealVO> page = setmealMapper.getPage(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //拿出每个id
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);
            //判断套餐有没有起售，如果有不能删除
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                //中断，抛异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

//        //判断套餐是否存在于套餐菜品表，如果有不能删
//        List<Long> dishId = setmealDishMapper.getSetmealIdsByDishId(ids);
//        if (dishId != null && dishId.size()>0){
//            throw  new DeletionNotAllowedException(MessageConstant.)
//        }

        //删除套餐
        ids.forEach(id->{
            setmealMapper.delete(id);
        });
    }


    //根据id修改套餐得状态
    @Override
    public void startOrStop(Integer status, Long id) {
        setmealMapper.startOrStop(status,id);
    }


    //修改套餐
    @Override
    public void update(SetmealDTO setmealDTO) {
            //修改套餐，要修改两个表，一个是套餐表，一个是套餐菜品表

        //套餐表
        //1.创建一个Setmeal对象
        Setmeal setmeal = new Setmeal();
        //2.给setmeal对象赋值
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //3.调用mapper方法，更新数据
        setmealMapper.updateSetmeal(setmeal);

        //套餐菜品表
            //因为套餐和菜品是多对多得关系
        //1.所以需要根据套餐得id来删除中间表得数据
        Long setmealId = setmeal.getId();

        //2.删除中间表得数据
        setmealDishMapper.deleteSetmealDishBySetmealId(setmealId);
        //3.获取修改后得数据，再重新插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //4.循环存入套餐和菜品关系表
        if (setmealDishes != null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });

            //往数据库中插入数据
            setmealDishMapper.insert(setmealDishes);
        }
    }


}
